#encoding:utf-8 

import os, sys, string 
import logging 
import sublime

def _get_ironpath():
	path_pack = sublime.packages_path()
	path_iron = os.path.join(path_pack, "iron-plugin")
	return path_iron

def crt_data(dataname, msglist, parentname="model.modelbase"):
    file_data = open(os.path.join(_get_ironpath(), "data.template"), "r")
    template_lines = file_data.readlines()
    file_data.close()

    ret = ""
    prolist = dict()
    procnt = 0
    for line in template_lines:
        if line.find("$") == 0:
            vv = line.split("&")
            prolist[vv[0][1:].strip()] = eval(vv[1]) 
        else:
            ret += line

    #proinit
    child1 = ""
    child2 = ""
    child3 = ""
    child4 = ""
    for msg in msglist:
        child1 += prolist["proinit"].replace("$msgname$", msg)
        child2 += prolist["proreg"].replace("$msgname$", msg)
        child3 += prolist["getfunc"].replace("$dataname$", dataname).replace("$msgname$", msg)
        child4 += prolist["onfunc"].replace("$dataname$", dataname).replace("$msgname$", msg)

    ret = ret.format(dataname = dataname, \
                proinit = child1, \
                proreg = child2, \
                getfunc = child3, \
                onfunc = child4)
    return ret


